package com.hankol.community.fragment

import android.app.Activity
import android.content.Intent
import com.baselibrary.activity.BaseFragment
import com.baselibrary.bean.CommentEnum
import com.baselibrary.extension.checkNotNull
import com.baselibrary.utils.GlideUtils
import com.baselibrary.utils.SpUtils
import com.hankol.community.PulCourseActivity
import com.okhttp.comment.CommentBean
import com.hankol.community.R
import com.hankol.community.arouter.CommunityHelp
import com.hankol.community.databinding.ActivityPulpitVideoBinding
import com.hankol.community.databinding.FragmentPulVideoCommentBinding
import com.hankol.community.viewmodel.PulpitViewModel
import com.okhttp.comment.CommentConstant
import com.okhttp.comment.IPCommentListener

class PulVideoCommentFragment(var communityHelp: CommunityHelp) : BaseFragment() {
    private lateinit var mBinding: FragmentPulVideoCommentBinding
    private lateinit var mViewModel: PulpitViewModel
    private lateinit var mActivity: Activity
    private var mDataBinding: ActivityPulpitVideoBinding? = null
    private var pulVideoId: Int = 0
    override fun init() {
        mActivity = activity as PulCourseActivity
        mBinding = getDataBinding<FragmentPulVideoCommentBinding>()
        mViewModel = getFragmentViewModelProvider(this).get(PulpitViewModel::class.java)

        GlideUtils.loadImg_101(context, SpUtils.getInstance().loadUserBean.data.icon, mBinding.circlePulVideoComment)
        mBinding.commentPulVideo.setAdapter(mActivity, IPCommentListener(mActivity, mBinding.commentPulVideo))
        initListener()
    }
    override fun getLayoutId(): Int {
        return R.layout.fragment_pul_video_comment
    }

    fun setPulCourseId(pulVideoId: Long) {
        this.pulVideoId = pulVideoId.toInt()
    }

    fun setBinding(mDataBinding: ActivityPulpitVideoBinding) {
        this.mDataBinding = mDataBinding
    }

    fun setData(pager: Int, bean: CommentBean.DataBean?) {
        if (bean != null) {

            if (bean.normalList.checkNotNull()) {
                mBinding.commentPulVideo.commentNetData(bean, pager)
                return
            }
        }
        if (pager == 1) mBinding.commentPulVideo.isEmptyView(false)

    }

    fun initListener() {
        mBinding.llPulVideoComment.setOnClickListener {
            //进行评论
            mDataBinding?.let {
                communityHelp.openCommentActivity(mActivity, CommentEnum.COMMENT_FORUM_COURSE.commentType, pulVideoId, it.includePulVideoComment.tvCommentSaySomething.text
                        .toString()
                        .trim(), "", false, CommentConstant.COMMENT_REQUESTCODE)
            }
        }
        mBinding.commentPulVideo.setEmptyViewListener {
            //进行评论
            mDataBinding?.let {
                communityHelp.openCommentActivity(mActivity, CommentEnum.COMMENT_FORUM_COURSE.commentType, pulVideoId, it.includePulVideoComment.tvCommentSaySomething.text
                        .toString()
                        .trim(), "", false, CommentConstant.COMMENT_REQUESTCODE)
            }
        }
    }

    /**
     * 评论完成回调
     */
    fun activityResult(requestCode: Int, resultCode: Int, data: Intent?, pulpitData: CommentBean.DataBean?): CommentBean.DataBean? {
        mBinding.commentPulVideo.commentOnResult(requestCode, resultCode, data)
            if (pulpitData != null) {
                mDataBinding?.let {
                    mBinding.commentPulVideo.setSayContent(requestCode, resultCode, data, it.includePulVideoComment.tvCommentSaySomething, CommentConstant.COMMENT_DEF_CONTENT_VERSION_1)
                    val commentNum: Int = mBinding.commentPulVideo.upCommentNum(requestCode, resultCode, data, pulpitData.totalCount, it.includePulVideoComment.tvCommentComCount)
                    if (commentNum != -1) pulpitData.totalCount = commentNum
                    return pulpitData
                }
            }
        return null
    }
}